//
//  UIView+DF.m
//  换换圈
//
//  Created by 艾柯斯 on 14-11-13.
//  Copyright (c) 2014年 艾柯斯. All rights reserved.
//

#import "UIView+DF.h"
#import "DFConst.h"
#import <objc/runtime.h>


@interface UIView()

@property (nonatomic,strong) UIView* screenBackgroundView;

@end

@implementation UIView (DF)

#pragma mark 屏幕相关
#pragma mark 屏幕运行时
-(UIView *)screenBackgroundView {
    return objc_getAssociatedObject(self,@selector(screenBackgroundView));
}

-(void)setScreenBackgroundView:(UIView *)screenBackgroundView {
    objc_setAssociatedObject(self, @selector(screenBackgroundView),
                             screenBackgroundView,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



-(void)showToScreen:(void (^)(UIView *))willShow didShow:(void (^)(UIView *))didShow {
    UIView* screenBackgroundView = self.screenBackgroundView;
    if(screenBackgroundView) {
        return;
    }
    
    screenBackgroundView = [[UIView alloc]initWithFrame:(CGRect){CGPointZero,ScreenSize}];
    self.screenBackgroundView = screenBackgroundView;
    screenBackgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];

    if(willShow) {
        willShow(screenBackgroundView);
    }
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [screenBackgroundView addSubview:self];
    [window addSubview:screenBackgroundView];

    if(didShow) {
        didShow(screenBackgroundView);
    }
    
    
    
}


-(void)showToScreen {
    [self showToScreen:nil didShow:nil];
}

-(void)hideForScreen {
    [self hideForScreen:nil didHide:nil];
}

-(void)hideForScreen:(void (^)(UIView *))willHide didHide:(void (^)(UIView *))didHide {
    UIView* screenBackgroundView = self.screenBackgroundView;
    if(!screenBackgroundView) {
        return;
    }
    if(willHide) {
        willHide(screenBackgroundView);
    }
    [screenBackgroundView removeFromSuperview];
    self.screenBackgroundView = nil;
    if(didHide) {
        didHide(screenBackgroundView);
    }
    
}

#pragma mark Frame相关
-(CGFloat)x {
    return self.frame.origin.x;
}


-(void)setX:(CGFloat)x {
    CGRect frame =  self.frame;
    frame.origin.x = x;
    self.frame = frame;
}


-(CGFloat)centerX {
    return self.center.x;
}


-(void)setCenterX:(CGFloat)centerX {
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}



-(CGFloat)y {
    return self.frame.origin.y;
}

-(void)setY:(CGFloat)y {
    CGRect frame =  self.frame;
    frame.origin.y = y;
    self.frame = frame;
}


-(CGFloat)centerY {
    return self.center.y;
}




-(void)setCenterY:(CGFloat)centerY {
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}



-(CGFloat)width {
    return self.frame.size.width;
}

-(void)setWidth:(CGFloat)width {
    CGRect frame =  self.frame;
    frame.size.width = width;
    self.frame = frame;
}

-(CGFloat)height {
    return self.frame.size.height;
}

-(void)setHeight:(CGFloat)height {
    CGRect frame =  self.frame;
    frame.size.height = height;
    self.frame = frame;
}


-(CGFloat)left {
    return self.frame.origin.x;
}

-(void)setLeft:(CGFloat)left {
    CGRect frame =  self.frame;
    frame.origin.x = left;
    self.frame = frame;
}

-(CGFloat)right {
    CGRect frame =  self.frame;
    return frame.origin.x + frame.size.width;
}

-(void)setRight:(CGFloat)right {
    CGRect frame =  self.frame;
    frame.origin.x =  right - frame.size.width;
    self.frame = frame;
}

-(CGFloat)top {
    return self.frame.origin.y;
}

-(void)setTop:(CGFloat)top {
    CGRect frame =  self.frame;
    frame.origin.y = top;
    self.frame = frame;
}

-(CGFloat)bottom {
    CGRect frame =  self.frame;
    return frame.origin.y  + frame.size.height;
    
}

-(void)setBottom:(CGFloat)bottom {
    CGRect frame =  self.frame;
    frame.origin.y = bottom- frame.size.height;
    self.frame = frame;
}


-(CGSize)size {
    return self.frame.size;
}

-(void)setSize:(CGSize)size {
    CGRect frame =  self.frame;
    frame.size = size;
    self.frame = frame;
}


-(CGPoint)origin {
    return self.frame.origin;
}

-(void)setOrigin:(CGPoint)origin {
    CGRect frame =  self.frame;
    frame.origin = origin;
    self.frame = frame;
}

#pragma mark 子视图
- (void)removeSubviews {
    for (UIView* subview in self.subviews) {
        [subview removeFromSuperview];
    }
}

- (void)removeSubviewsWithClassFromString:(NSString*)className recursion:(BOOL)recursion {
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(className)]) {
            [subview removeFromSuperview];
        }
        if(recursion) {
            [self removeSubviewsWithClassFromString:className recursion:recursion];
        }
    }
    
}



#pragma mark 样式相关
- (void)round {
    self.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f;
    self.clipsToBounds = YES;
}

#pragma mark 遮罩相关
+ (instancetype)coverWithBackgroundColor:(UIColor*)backgroundColor {
    UIView* cover = [[UIView alloc] initWithFrame:(CGRect){CGPointZero,ScreenSize}];
    cover.backgroundColor = backgroundColor;
    cover.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    return cover;
}

+ (instancetype)coverWithFrame:(CGRect)frame backgroundColor:(UIColor*)backgroundColor {
    UIView* cover = [[UIView alloc] initWithFrame:frame];
    cover.backgroundColor = backgroundColor;
    cover.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    return cover;
}

- (instancetype)addToWindow {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    return self;
}

@end
